package com.yinp.proapp.module.wanandroid.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.fragment.PresenterWanAndroidFragment;
import com.yinp.proapp.databinding.FragmentWanQuestionAnswerBinding;
import com.yinp.proapp.databinding.ItemQuestionAnswerListBinding;
import com.yinp.proapp.module.wanandroid.bean.WanQuestionAnswerBean;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanData2;
import com.yinp.proapp.module.wanandroid.web.retrofit.WanObserver2;
import com.yinp.proapp.utils.JumpWebUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :yinpeng
 * date      :2021/8/27
 * package   :com.yinp.proapp.module.wanandroid.fragment
 * describe  :
 */
public class WanQuestionAnswerFragment extends PresenterWanAndroidFragment<FragmentWanQuestionAnswerBinding> {
    public static WanQuestionAnswerFragment getInstance() {
        WanQuestionAnswerFragment wanQuestionAnswerFragment = new WanQuestionAnswerFragment();
        Bundle bundle = new Bundle();
        wanQuestionAnswerFragment.setArguments(bundle);
        return wanQuestionAnswerFragment;
    }

    private List<WanQuestionAnswerBean.DatasBean> mDataList = new ArrayList<>();
    private CommonAdapter<WanQuestionAnswerBean.DatasBean> mAdapter;
    private int mPage = 0;

    @Override
    protected void initViews(View view) {
        initRecycler();
        refresh();
        getSquareList(true);
    }

    private void initRecycler() {
        mAdapter = new CommonAdapter<WanQuestionAnswerBean.DatasBean>(mContext,mDataList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemQuestionAnswerListBinding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, WanQuestionAnswerBean.DatasBean item) {
                ItemQuestionAnswerListBinding binding = (ItemQuestionAnswerListBinding) holder.getBinding();
                if (item.isCollect()) {
                    binding.ivCollect.setSelected(true);
                } else {
                    binding.ivCollect.setSelected(false);
                }
                binding.tvTitle.setText(item.getTitle());
                binding.tvSharePerson.setText(TextUtils.isEmpty(item.getAuthor()) ? "暂无" : item.getAuthor());
                binding.tvTime.setText(item.getNiceDate());
                binding.tvClassify.setText(item.getChapterName());
            }
        };
        mAdapter.setOnItemClickListener((position, view) -> {
            WanQuestionAnswerBean.DatasBean item = mDataList.get(position);
            if (!TextUtils.isEmpty(item.getLink())) {
                JumpWebUtils.startWebView(getContext(), item.getTitle(), item.getLink());
            } else {
                showToast("缺少链接地址");
            }
        });
        bd.baseRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        bd.baseRecycle.setHasFixedSize(true);
        bd.baseRecycle.setAdapter(mAdapter);
    }

    private void refresh() {
        //为下来刷新添加事件
        bd.baseRefresh.setOnRefreshListener(refreshLayout -> {
            mPage = 0;
            getSquareList(false);
        });
        //为上拉加载添加事件
        bd.baseRefresh.setOnLoadMoreListener(refreshLayout -> {
            mPage++;
            getSquareList(false);
        });
    }

    /**
     * 获取导航列表
     */
    private void getSquareList(boolean isLoad) {
        if (isLoad) {
            showLoading("加载中...");
        }
        presenter.getQuestionAnswerList(mPage, new WanObserver2<WanData2<WanQuestionAnswerBean>>() {
            @Override
            public void onSuccess(WanData2<WanQuestionAnswerBean> o) {
                if (o.getData() != null) {
                    List<WanQuestionAnswerBean.DatasBean> list = o.getData().getDatas();
                    if (list != null && list.size() > 0) {
                        int length = mDataList.size();
                        if (mPage == 0) {
                            mDataList.clear();
                            mDataList.addAll(list);
                            mAdapter.notifyDataSetChanged();
                            bd.baseRefresh.finishRefresh(true);
                        } else {
                            mDataList.addAll(list);
                            mAdapter.notifyItemRangeChanged(length - 1, mDataList.size());
                            bd.baseRefresh.finishLoadMore(true);
                        }
                        bd.baseRefresh.setVisibility(View.VISIBLE);
                        bd.bottom.noLl.setVisibility(View.GONE);
                    } else {
                        if (!isLoad && mPage == 0) {
                            bd.baseRefresh.finishRefresh(false);
                        } else if (!isLoad && mPage > 0) {
                            bd.baseRefresh.finishLoadMore(false);
                        }
                    }
                } else {
                    if (!isLoad && mPage == 0) {
                        bd.baseRefresh.finishRefresh(false);
                        bd.baseRefresh.setVisibility(View.GONE);
                        bd.bottom.noLl.setVisibility(View.VISIBLE);
                    } else if (!isLoad && mPage > 0) {
                        bd.baseRefresh.finishLoadMore(false);
                    }
                }
                if (isLoad) {
                    hideLoading();
                }
            }

            @Override
            public void onCodeFail(String msg, int code) {
                if (!isLoad && mPage == 0) {
                    bd.baseRefresh.finishRefresh(false);
                } else if (!isLoad && mPage > 0) {
                    bd.baseRefresh.finishLoadMore(false);
                }
                if (isLoad) {
                    hideLoading();
                }
                showToast(msg);
            }
        });
    }
}
